package database;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.io.*;

import database.tree.BPlusTreeHelper;
import database.tree.Key;

public class Index implements Serializable {
	private String indexName;
	private HashMap<String, String> result;
	private BPlusTreeHelper bPlusTree = new BPlusTreeHelper();

	public Index(String indexName, HashMap<String, String> result) {
		this.indexName = indexName;
		this.setResult(result);
		for (Map.Entry<String, String> em : result.entrySet()) {
			bPlusTree.addNode(em.getKey(), Integer.valueOf(em.getValue())); // key is the attribute value,
												// value is the recordId.
		}
	}

	public void dumpIndex() {
		try {
			FileOutputStream fos = new FileOutputStream(this.indexName);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(this);
			oos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String viewIndex() {
		if( bPlusTree != null )
			return bPlusTree.startPrint();
		else
			return null;
	}

	public Iterator<Integer> iterator(String key) {
		return new IndexIterator(key);
	}

	public BPlusTreeHelper getbPlusTree() {
		return bPlusTree;
	}

	public void setbPlusTree(BPlusTreeHelper bPlusTree) {
		this.bPlusTree = bPlusTree;
	}

	public HashMap<String, String> getResult() {
		return result;
	}

	public void setResult(HashMap<String, String> result) {
		this.result = result;
	}

	private class IndexIterator implements Iterator<Integer> {
		List<Key> keyList = new LinkedList<Key>();
		
		public IndexIterator(String keyValue) {
			
		}

		@Override
		public boolean hasNext() {
			return false;
		}

		@Override
		public Integer next() {
			return 0;
		}

		@Override
		public void remove() {
		}

	}

}
